package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.InspectionRule;
import com.neusoft.neuiotms.dcom.model.QInspectionRule;
import com.neusoft.neuiotms.dcom.model.enums.InspectionRuleStatus;
import com.neusoft.neuiotms.dcom.repository.CustomizedInspectionRuleRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInspectionRuleRepositoryImpl extends AbstractRepository
        implements CustomizedInspectionRuleRepository {

    @Override
    public List<InspectionRule> index(final Searchable searchable) {

        final QInspectionRule qInspectionRule = QInspectionRule.inspectionRule;

        final Boolean isAll = searchable.getBooleanValue("isAll");
        final BooleanBuilder where = this.buildWhere(searchable);
        if ((isAll == null) || !isAll) {
            final List<Long> ids = this.findMaxIdsGroupByCode();
            where.and(qInspectionRule.delFlag.eq(0)).and(qInspectionRule.id.in(ids));
        }
        final JPAQuery<InspectionRule> query = this.query().selectFrom(qInspectionRule);
        query.where(where);
        return query.fetch();
    }

    private List<Long> findMaxIdsGroupByCode() {
        final QInspectionRule qInspectionRule = QInspectionRule.inspectionRule;
        final JPAQuery<Long> dquery =
                this.query().select(qInspectionRule.id.max()).from(qInspectionRule)
                        .groupBy(qInspectionRule.code);
        return dquery.fetch();
    }

    @Override
    public Page<InspectionRule> page(final Searchable searchable, final Pageable pageable) {
        final List<Long> ids = this.findMaxIdsGroupByCode();
        final QInspectionRule qInspectionRule = QInspectionRule.inspectionRule;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InspectionRule> query = this.query().selectFrom(qInspectionRule);
        where.and(qInspectionRule.id.in(ids));
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QInspectionRule qInspectionRule = QInspectionRule.inspectionRule;
        // 部门codes集合
        final String[] orgCodes = searchable.getStrArray("orgCodes");
        if ((orgCodes != null) && (orgCodes.length > 0)) {
            where.and(super.inStrings(qInspectionRule.orgCode, searchable, "orgCodes"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.and(super.containsStr(qInspectionRule.name, searchable, "name"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            where.and(super.containsStr(qInspectionRule.code, searchable, "code"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("orgCode"))) {
            where.and(super.containsStr(qInspectionRule.orgCode, searchable, "orgCode"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("usercode"))) {
            where.and(super.equalsStr(qInspectionRule.userCode, searchable, "usercode"));//创建人
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("startDateTimeBegin"))) {
            where.and(
                    super.afterOrEqualsDate(qInspectionRule.startDateTime, searchable, "startDateTimeBegin"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("startDateTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(qInspectionRule.startDateTime, searchable, "startDateTimeEnd"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("endDateTimeBegin"))) {
            where.and(super.afterOrEqualsDate(qInspectionRule.endDateTime, searchable,
                    "endDateTimeBegin"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("endDateTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(qInspectionRule.endDateTime, searchable,
                    "endDateTimeEnd"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            where.and((super.equalsEnum(qInspectionRule.status, searchable, "status",
                    value -> InspectionRuleStatus.valueOf(value))));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("globalSearch"))) {
            final BooleanBuilder whereGlobal = new BooleanBuilder();

            whereGlobal.or(super.containsStr(qInspectionRule.name, searchable, "globalSearch"));
            whereGlobal.or(super.containsStr(qInspectionRule.userName, searchable, "globalSearch"));
            where.and(whereGlobal);
        }
        if (searchable.getIntValue("delFlag") != null) {
            where.and(super.equalsInt(qInspectionRule.delFlag, searchable, "delFlag"));
        }
        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return InspectionRule.class;
    }

}
